import { AbstractParser, ParserOptions, ParseContext } from './BaseParser';
import { ExtractionTask, ExtractionResult } from '../types';

export class IosParser extends AbstractParser {
  /**
   * 解析iOS本地化文件
   * @param content 文件内容
   * @param context 解析上下文
   */
  public async parse(content: string, context: ParseContext): Promise<ExtractionResult> {
    try {
      // 验证内容
      if (this.shouldIgnore(content)) {
        return this.createEmptyResult(context.task.taskId);
      }

      // 提取翻译键
      const translations = await this.extractTranslations(content, context);

      // 创建结果
      return {
        taskId: context.task.taskId,
        status: 'success',
        startTime: new Date().toISOString(),
        endTime: new Date().toISOString(),
        statistics: {
          totalFiles: 1,
          processedFiles: 1,
          extractedKeys: Object.keys(translations).length,
          errors: 0,
          processingTime: 0
        },
        translations,
        analysis: {
          coverage: {
            overall: 0,
            byLanguage: {},
            byModule: {},
            missingKeys: [],
            redundantKeys: []
          },
          usage: {
            highFrequency: [],
            lowFrequency: [],
            unusedKeys: []
          },
          modules: {},
          quality: {
            duplicates: [],
            namingIssues: [],
            contextIssues: []
          }
        },
        errors: [],
        suggestions: []
      };

    } catch (error) {
      return {
        taskId: context.task.taskId,
        status: 'failed',
        startTime: new Date().toISOString(),
        endTime: new Date().toISOString(),
        statistics: {
          totalFiles: 1,
          processedFiles: 0,
          extractedKeys: 0,
          errors: 1,
          processingTime: 0
        },
        translations: [],
        analysis: {
          coverage: {
            overall: 0,
            byLanguage: {},
            byModule: {},
            missingKeys: [],
            redundantKeys: []
          },
          usage: {
            highFrequency: [],
            lowFrequency: [],
            unusedKeys: []
          },
          modules: {},
          quality: {
            duplicates: [],
            namingIssues: [],
            contextIssues: []
          }
        },
        errors: [{
          file: context.filePath,
          message: (error as Error).message,
          stack: (error as Error).stack,
          severity: 'error'
        }],
        suggestions: []
      };
    }
  }

  /**
   * 提取翻译键
   * @param content 文件内容
   * @param context 解析上下文
   */
  private async extractTranslations(content: string, context: ParseContext): Promise<ExtractionResult['translations']> {
    const translations: ExtractionResult['translations'] = {};
    const lines = content.split('\n');
    let currentKey = '';
    let currentValue = '';
    let currentComment = '';
    let lineNumber = 0;

    for (const line of lines) {
      lineNumber++;

      // 跳过空行
      if (!line.trim()) {
        continue;
      }

      // 处理注释
      if (line.trim().startsWith('/*')) {
        currentComment = line.trim().replace(/^\/\*|\*\/$/g, '').trim();
        continue;
      }

      // 处理键值对
      const match = line.match(/^"([^"]+)"\s*=\s*"([^"]+)";$/);
      if (match) {
        const [, key, value] = match;
        
        if (this.isValidKey(key) && this.isValidValue(value)) {
          const fileContext = this.extractContext(content, lineNumber, 0);

          translations[key] = {
            source: value,
            context: fileContext,
            module: this.getModuleFromPath(context.filePath),
            tags: this.extractTags(currentComment),
            occurrences: [{
              file: context.filePath,
              line: lineNumber,
              column: 0,
              context: fileContext,
              scope: 'global',
              function: '',
              class: ''
            }],
            frequency: 1,
            lastUsed: new Date().toISOString(),
            translations: {
              [context.task.languages[0]]: {
                value,
                source: 'ios',
                lastModified: new Date().toISOString(),
                status: 'valid',
                quality: 1,
                comments: currentComment ? [currentComment] : []
              }
            },
            metadata: {
              createdAt: new Date().toISOString(),
              createdBy: 'system',
              lastModified: new Date().toISOString(),
              modifiedBy: 'system',
              version: context.task.version,
              platform: ['ios'],
              status: 'active'
            },
            analysis: {
              complexity: this.calculateComplexity(value),
              risk: this.assessRisk(value),
              suggestions: this.generateSuggestions(value),
              relatedKeys: [],
              dependencies: []
            }
          };
        }

        // 重置状态
        currentKey = '';
        currentValue = '';
        currentComment = '';
      }
    }

    return translations;
  }

  /**
   * 从路径获取模块名
   * @param filePath 文件路径
   */
  private getModuleFromPath(filePath: string): string {
    const parts = filePath.split('/');
    const lprojIndex = parts.indexOf('lproj');
    if (lprojIndex > 0) {
      return parts[lprojIndex - 1];
    }
    return 'unknown';
  }

  /**
   * 从注释中提取标签
   * @param comment 注释内容
   */
  private extractTags(comment: string): string[] {
    const tags: string[] = [];
    const tagRegex = /@(\w+)/g;
    let match;

    while ((match = tagRegex.exec(comment)) !== null) {
      tags.push(match[1]);
    }

    return tags;
  }

  /**
   * 计算复杂度
   * @param value 翻译值
   */
  private calculateComplexity(value: string): number {
    let complexity = 0;
    
    // 检查占位符数量
    const placeholderRegex = /%[0-9]*[sd]/g;
    const placeholders = value.match(placeholderRegex) || [];
    complexity += placeholders.length;

    // 检查HTML标签
    const htmlRegex = /<[^>]+>/g;
    const htmlTags = value.match(htmlRegex) || [];
    complexity += htmlTags.length;

    // 检查特殊字符
    const specialChars = value.match(/[&<>]/g) || [];
    complexity += specialChars.length;

    return complexity;
  }

  /**
   * 评估风险等级
   * @param value 翻译值
   */
  private assessRisk(value: string): 'high' | 'medium' | 'low' {
    const complexity = this.calculateComplexity(value);
    
    if (complexity > 5) {
      return 'high';
    } else if (complexity > 2) {
      return 'medium';
    }
    
    return 'low';
  }

  /**
   * 生成改进建议
   * @param value 翻译值
   */
  private generateSuggestions(value: string): string[] {
    const suggestions: string[] = [];

    // 检查占位符格式
    const placeholderRegex = /%[0-9]*[sd]/g;
    const placeholders = value.match(placeholderRegex) || [];
    if (placeholders.length > 0) {
      suggestions.push('Consider using named placeholders for better maintainability');
    }

    // 检查HTML标签
    const htmlRegex = /<[^>]+>/g;
    const htmlTags = value.match(htmlRegex) || [];
    if (htmlTags.length > 0) {
      suggestions.push('Consider using text styling attributes instead of HTML tags');
    }

    // 检查特殊字符
    const specialChars = value.match(/[&<>]/g) || [];
    if (specialChars.length > 0) {
      suggestions.push('Consider using HTML entities for special characters');
    }

    return suggestions;
  }

  /**
   * 创建空结果
   * @param taskId 任务ID
   */
  private createEmptyResult(taskId: string): ExtractionResult {
    return {
      taskId,
      status: 'success',
      startTime: new Date().toISOString(),
      endTime: new Date().toISOString(),
      statistics: {
        totalFiles: 1,
        processedFiles: 0,
        extractedKeys: 0,
        errors: 0,
        processingTime: 0
      },
      translations: [],
      analysis: {
        coverage: {
          overall: 0,
          byLanguage: {},
          byModule: {},
          missingKeys: [],
          redundantKeys: []
        },
        usage: {
          highFrequency: [],
          lowFrequency: [],
          unusedKeys: []
        },
        modules: {},
        quality: {
          duplicates: [],
          namingIssues: [],
          contextIssues: []
        }
      },
      errors: [],
      suggestions: []
    };
  }
} 